<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>通过ajax来发送json请求</title>
</head>
<body>
          姓名： <input type="text" id="name"></br>
          年龄： <input type="text" id="age"></br>
          <input type="button" value="提交" id="submit">

          <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

          <script>
               $("#submit").click(
                 function(){
                    $.ajax({
                        type:"post",
                        url:"json",
                        contentType:"application/json",
                        data:JSON.stringify({
                            name:$("#name").val(),
                            age:$("#age").val()
                        }),
                        success:function(body){
                            console.log(body);

                        },
                        error:function(){
                            console.log("error");
                        }
                    });
                 }
               );
          </script>
</body>
</html>